//
//  oldVoiceSearchViewController.m
//  laonianbang
//
//  Created by 勇拓 李 on 17/10/19.
//  Copyright © 2017年 zaiyou. All rights reserved.
//

#import "oldVoiceSearchViewController.h"
#import "oldRippleView.h"
#import "oldVoiceView.h"
#import "QNuploader.h"
#import "oldVoiceManager.h"
#import "oldGifView.h"
#import "oldOrderInfo.h"
#import "oldMapViewController.h"
#import "oldLocationManager.h"

@interface oldVoiceSearchViewController ()<oldRipperViewDelegate>
@property (nonatomic, strong)oldRippleView *rippleView;
@property (nonatomic, strong)oldVoiceView *voiceView;
@property (nonatomic, strong)UIView *line;
@property (nonatomic, strong)UIButton *btn_del;
@property (weak, nonatomic) IBOutlet UIButton *btn_publish;
@property (weak, nonatomic) IBOutlet UIButton *btn_notice;

//@property (nonatomic, strong)oldSupporterMatchCard *card;

@end

@implementation oldVoiceSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - initView
- (void)initView
{
    _btn_publish.layer.cornerRadius = 6.f;
    _btn_publish.hidden = YES;
    [_btn_publish addTarget:self action:@selector(publish:) forControlEvents:UIControlEventTouchDown];
    
    [_btn_notice addTarget:self action:@selector(cancelSearch:) forControlEvents:UIControlEventTouchDown];
    
    [self addRippleView];
}

- (oldRippleView *)rippleView
{
    if (!_rippleView) {
        _rippleView = [[oldRippleView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
        [_rippleView showWithRipple];
        _rippleView.delegate = self;
    }
    
    return _rippleView;
}

- (oldVoiceView *)voiceView
{
    if (!_voiceView) {
        _voiceView = [[[NSBundle mainBundle] loadNibNamed:@"oldVoiceView" owner:self options:nil] objectAtIndex:0];
        _voiceView.frame = CGRectMake(20, _btn_notice.frame.origin.y + 80, 300, 58);
    }
    
    return _voiceView;
}

- (UIView *)line
{
    if (!_line) {
        _line = [[UIView alloc] initWithFrame:CGRectMake(15, _voiceView.frame.origin.y + 60, SCREEN_WIDTH - 30, 1)];
        _line.backgroundColor = Color_With_Rgb(230,230,230,0.5);
    }
    
    return _line;
}

- (UIButton *)btn_del
{
    if (!_btn_del) {
        _btn_del = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 100, _voiceView.frame.origin.y + 20, 80, 14)];
        [_btn_del setTitleColor:Color_With_Rgb(77,77,77,1) forState:UIControlStateNormal];
        [_btn_del setTitle:@"重新录制" forState:UIControlStateNormal];
        _btn_del.titleLabel.font = [UIFont systemFontOfSize: 15.0];
        [_btn_del addTarget:self action:@selector(btn_del_tapped:) forControlEvents:UIControlEventTouchDown];
    }
    
    return _btn_del;
}

#pragma mark - network
- (void)publish:(id)sender
{
    WeakSelf
    [oldHub waiting:@"上传中.."];
    [QiNiuShareInstance uploadImageToQNFilePath:[oldVoice changeCafToMp3] type:Voice_file block:^(NSString *url) {
        [oldHub hideHUD];
        if (url) {
            [weakSelf publishSuccess:url];
        }
    }];
}

#pragma mark - 发布成功，寻找中
- (void)addRippleView
{
    [oldGifView endLoading:self.view];
    
    [self.view addSubview:self.rippleView];
    
    CGRect frame = CGRectMake((SCREEN_WIDTH - 200)/2, (SCREEN_WIDTH - 200)/2, 200, 200);
    self.rippleView.frame = frame;
    
    _btn_notice.userInteractionEnabled = NO;
    [_btn_notice setTitle:@"按住说话" forState:UIControlStateNormal];
    _btn_notice.titleLabel.text = @"按住说话";
}

- (void)addGif
{
    [_rippleView removeFromSuperview];
    [_line removeFromSuperview];
    [_btn_del removeFromSuperview];
    [_voiceView removeFromSuperview];
    _btn_publish.hidden = YES;
    
    [oldGifView startLoading:self.view];
    
    _btn_notice.userInteractionEnabled = YES;
    [_btn_notice setTitle:@"取消寻找" forState:UIControlStateNormal];
    _btn_notice.titleLabel.text = @"取消寻找";
}

- (void)cancelSearch:(id)sender
{
    [self addRippleView];
    
    [self.view addSubview:self.voiceView];
    [self.view addSubview:self.line];
    [self.view addSubview:self.btn_del];
    
    _btn_publish.hidden = NO;
}

- (void)publishSuccess:(NSString *)url
{
    [self addGif];
    
    WeakSelf
    
    oldLocation *location = [oldLocation_SINGLETON getLocation];
    if (location.lat && location.lng) {
        NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:location.lat, @"lat", location.lng, @"lng", url, @"voiceUrl", @"天府三街", @"location", nil];
        
        [OLDNetWork httpRequestWithPostPut:URL_SendRequest params:param success:^(NSDictionary *dic) {
            [weakSelf matchSever:dic];
        } error:^(NSString *error) {
            [oldHub warningUser:error];
        }];
    }
    else
    {
        [oldHub warningUser:@"定位未成功，请重试"];
    }
}

- (void)matchSever:(NSDictionary *)dic
{
    oldOrderInfo *info = [[oldOrderInfo alloc] init];
    
//    _card = [[oldSupporterMatchCard alloc] initWithorderInfo:info];
//    
//    _card.delegate = self;
}

#pragma mark - coupleDelegate
- (void)gotoMapView:(oldOrderInfo *)info
{
//    [_card removeSelf];
    oldMapViewController *vc = [oldMapViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)swapSuppor
{
//    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"更换工作人员" message:@"确定要更换工作人员吗？" preferredStyle:UIAlertControllerStyleAlert];
    
    
    
//    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//        _card.superview.hidden = NO;
//    }]];
//    
//    
//    
//    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        [_card changeSuppor];
//    }]];
//    
//    [self presentViewController:alertController animated:YES completion:nil];
//    
//    _card.superview.hidden = YES;
}

#pragma mark - voiceDelegate
- (void)oldRipperViewBtnClick:(BOOL)on error:(BOOL)isError
{
    if (on) {
        [self.voiceView removeView];
        [self.line removeFromSuperview];
        [self.btn_del removeFromSuperview];
        _btn_publish.hidden = YES;
    }
    else
    {
        if (!isError) {
            [self.view addSubview:self.voiceView];
            [self.view addSubview:self.line];
            [self.view addSubview:self.btn_del];
            [_voiceView beginWithRecord];
            _btn_publish.hidden = NO;
        }
    }
}

- (void)btn_del_tapped:(id)sender
{
    [self.voiceView removeView];
    [self.line removeFromSuperview];
    [self.btn_del removeFromSuperview];
    _btn_publish.hidden = YES;
}

@end
